﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using GameBoardViewer.Board3D;


namespace GameBoardViewer.Common
{
    public enum enumButtonDockStyle { TopLeft, TopRight, BottomLeft, BottomRight }

    public class GameBoardButton : IBoardControl
    {
        protected SpriteBatch _batch;
        protected Game _game = null;
        protected IMainBoard _mainBoard;
        
        protected Texture2D _buttonPicture;
        protected Texture2D _colorHover;
        protected Texture2D _colorPressed;

        protected int _marginX;
        protected int _marginY;
        protected int _width;
        protected int _height;
        protected enumButtonDockStyle _dockStyle;

        protected bool _isHover;
        protected bool _isPressed;
        protected ButtonState _lastButtonState;
        protected bool _isCapturing;
            
        public GameBoardButton(Game game, IMainBoard mainBoard)
        {
            _mainBoard = mainBoard;
            _game = game;
            _isHover = false;
            _isPressed = false;
            _isCapturing = false;
            _lastButtonState = ButtonState.Released;
        }

        public event EventHandler ButtonPressed;

        public void LoadContent(string sTexture, int marginX, int marginY, enumButtonDockStyle dockStyle, 
                    Color clrHover, Color clrPressed)
        {
            _batch = new SpriteBatch(_game.GraphicsDevice);

            _marginX = marginX;
            _marginY = marginY;
            _dockStyle = dockStyle;

            _buttonPicture = _game.Content.Load<Texture2D>(sTexture);
            _width = _buttonPicture.Width;
            _height = _buttonPicture.Height;

            Color[] data = new Color[1];
            _colorHover = new Texture2D(_game.GraphicsDevice, 1, 1);
            _colorHover.GetData<Color>(data);
            data[0] = clrHover;
            _colorHover.SetData<Color>(data);

            data = new Color[1];
            _colorPressed = new Texture2D(_game.GraphicsDevice, 1, 1);
            _colorPressed.GetData<Color>(data);
            data[0] = clrPressed;
            _colorPressed.SetData<Color>(data);
        }

        protected virtual void DrawPicture()
        {
            Rectangle rect = GetButtonRect();

            _batch.Begin();
            _batch.Draw(_buttonPicture, rect, Color.White);
            _batch.End();
        }

        public virtual void Draw()
        {
            Rectangle rect = GetButtonRect();

            DrawPicture();

            _batch.Begin();

            if (_isHover == true /*&& _isPressed == false*/)
            {
                _batch.Draw(_colorHover, rect, Color.White);
            }
            /*if (_isPressed == true)
            {
                _batch.Draw(_colorPressed, rect, Color.White);
            }*/

            _batch.End();
        }

        public virtual bool Update(MouseState mouse)
        {
            return false;
        }

        public virtual void OnLButtonDown(Point point)
        {
            if (_isHover)
            {
                _isPressed = true;
                _isCapturing = true;
                _mainBoard.SetCapture(this);
            }
        }

        public virtual void OnLButtonUp(Point point)
        {
            _mainBoard.ReleaseCapture();
            _isCapturing = false;
            _isPressed = false;
            if (_isHover)
            {
                if (ButtonPressed != null)
                {
                    ButtonPressed(this, new EventArgs());
                }
            }
        }

        public virtual void OnMouseMoving(Point point)
        {
            Rectangle rect = GetButtonRect();

            if (rect.Contains(point) == true)
                _isHover = true;
            else
                _isHover = false;
        }

        public virtual void OnRButtonDown(Point point)
        {
        }

        public virtual void OnRButtonUp(Point point)
        {
        }


        protected Rectangle GetButtonRect()
        {
            int x = _marginX;
            if (_dockStyle == enumButtonDockStyle.BottomRight || _dockStyle == enumButtonDockStyle.TopRight)
                x = _mainBoard.GameBoardWidth - _marginX - _width;

            int y = _marginY;
            if (_dockStyle == enumButtonDockStyle.BottomLeft || _dockStyle == enumButtonDockStyle.BottomRight)
                y = _mainBoard.GameBoardHeight - _marginY - _height;

            return new Rectangle(x, y, _width, _height);
        }
    }
}
